﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace BanHangCshap.Class
{
    class TinhGiaVon:Database
    {
        public Boolean nhapTruocXuatTruocCu(string mahang)
        {
            /*
            string delete1 = "delete PhieuNhapCTTam where mahang = '" + mahang + "'";
            if (executeQuery(delete1) == false)
                return false;
            string delete2 = "delete PhieuXuatCTTam where mahang = '" + mahang + "'";
            if (executeQuery(delete2) == false)
                return false;

            string insert = "insert into PhieuNhapCTTam  select a.manhapxuat, mahang, CONVERT(decimal,(dgnhapxuat/slquydoi)) as dgnhapxuat, slnhapxuat,id,sort from PhieuNhapXuatCT a, PhieuNhapXuat b where a.mahang = '" + mahang + "' and a.manhapxuat = b.manhapxuat and loainhapxuat = 'PN' order by sort";
            if (executeQuery(insert) == false)
                return false;
            string dspx = "select a.manhapxuat, mahang, slnhapxuat, CONVERT(decimal,(dgnhapxuat/slquydoi)) as dgnhapxuat from PhieuNhapXuatCT a, PhieuNhapXuat b where a.mahang = '" + mahang + "' and a.manhapxuat = b.manhapxuat and loainhapxuat = 'PX' order by sort";



            Boolean ok = true;
            DataTable dtpx = getDataTable(dspx);
            if (dtpx.Rows.Count > 0)
            {

                foreach (DataRow row_px in dtpx.Rows)
                {


                    string manhapxuat = row_px["manhapxuat"].ToString().Trim();
                    float slnhapxuat = float.Parse(row_px["slnhapxuat"].ToString().Trim());
                    string pnchuaxuathet = "select manhapxuat,mahang,dgnhap,slchuaxuat ";
                    pnchuaxuathet += "from PhieuNhapCTTam where mahang = '" + mahang + "' and slchuaxuat > 0 order by id";
                    float slyeucau = slnhapxuat;
                    int dgbanvon = 0;
                    int tgban = 0;
                    DataTable dtpn = getDataTable(pnchuaxuathet);
                    if (dtpx.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dtpn.Rows)
                        {
                            string sql = "";
                            string submanhapxuat = dr["manhapxuat"].ToString();

                            if (slyeucau - float.Parse(dr["slchuaxuat"].ToString()) <= 0)
                            {
                                tgban = tgban + (int)(slyeucau * int.Parse(dr["dgnhap"].ToString()));
                                slyeucau = slyeucau - float.Parse(dr["slchuaxuat"].ToString());
                                float slchuaxuat = float.Parse(dr["slchuaxuat"].ToString()) - slyeucau;

                                sql = "update PhieuNhapCTTam  set slchuaxuat = " + slchuaxuat + " where manhapxuat = '" + submanhapxuat + "' and mahang = '" + mahang + "'";
                                if (executeQuery(sql) == false)
                                {
                                    ok = false;
                                    break;
                                }
                                break;
                            }
                            else
                            {
                                tgban = tgban + (int)(float.Parse(dr["slchuaxuat"].ToString()) * int.Parse(dr["dgnhap"].ToString()));
                                slyeucau = slyeucau - float.Parse(dr["slchuaxuat"].ToString());

                                sql = "update PhieuNhapCTTam  set slchuaxuat = 0 where manhapxuat = '" + submanhapxuat + "' and mahang = '" + mahang + "'";
                                if (executeQuery(sql) == false)
                                {
                                    ok = false;
                                    break;
                                }
                            }
                        }
                        if (ok)
                        {
                            //PhieuXuatCTTam clspxtam = new PhieuXuatCTTam();
                            if (slnhapxuat != 0)
                            {
                                dgbanvon = (int)(tgban / slnhapxuat);
                            }
                            else
                                dgbanvon = 0;
                            string sqlthempxtam = "insert into PhieuXuatCTTam(manhapxuat,mahang,slxuat,dgxuatvon) ";
                            sqlthempxtam += " values ('" + manhapxuat + "','" + mahang + "'," + slnhapxuat + "," + dgbanvon + ")";
                            if (executeQuery(sqlthempxtam) == false)

                                ok = false;
                           
                        }
                    }
                }
            }

            
            return true;
              */
            /*-------------------22/02/2012-------------------------*/
            Boolean ok = true;
            try
            {
                string delete1 = "delete PhieuNhapCTTam where mahang = '" + mahang + "'";
                if (executeQuery(delete1) == false)
                    return false;
                string delete2 = "delete PhieuXuatCTTam where mahang = '" + mahang + "'";
                if (executeQuery(delete2) == false)
                    return false;

                string insert = "insert into PhieuNhapCTTam  select manhapxuatct,a.manhapxuat, mahang, convert(decimal,(dgnhapxuat/slquydoi)) as dgnhapxuat, slnhapxuat,id,sort from PhieuNhapXuatCT a, PhieuNhapXuat b where a.mahang = '" + mahang + "' and a.manhapxuat = b.manhapxuat and loainhapxuat = 'PN' order by sort";
                if (executeQuery(insert) == false)
                    return false;
                string dspx = "select manhapxuatct,a.manhapxuat, mahang, slnhapxuat, convert(decimal,(dgnhapxuat/slquydoi)) as dgnhapxuat from PhieuNhapXuatCT a, PhieuNhapXuat b where a.mahang = '" + mahang + "' and a.manhapxuat = b.manhapxuat and loainhapxuat = 'PX' order by sort";




                DataTable dtpx = getDataTable(dspx);
                if (dtpx.Rows.Count > 0)
                {

                    foreach (DataRow row_px in dtpx.Rows)
                    {

                        int manhapxuatct = 0;

                        manhapxuatct = int.Parse(row_px["manhapxuatct"].ToString());
                        string manhapxuat = row_px["manhapxuat"].ToString().Trim();
                        float slnhapxuat = float.Parse(row_px["slnhapxuat"].ToString().Trim());
                        string pnchuaxuathet = "select manhapxuatct,manhapxuat,mahang,dgnhap,slchuaxuat ";
                        pnchuaxuathet += "from PhieuNhapCTTam where mahang = '" + mahang + "' and slchuaxuat > 0 order by sort";
                        float slyeucau = slnhapxuat;
                        int dgbanvon = 0;
                        int tgban = 0;
                        DataTable dtpn = getDataTable(pnchuaxuathet);

                        foreach (DataRow dr in dtpn.Rows)
                        {
                            string sql = "";
                            int submanhapxuatct = int.Parse(dr["manhapxuatct"].ToString());
                            string submanhapxuat = dr["manhapxuat"].ToString();

                            if (slyeucau - float.Parse(dr["slchuaxuat"].ToString()) <= 0)
                            {
                                tgban = tgban + (int)(slyeucau * int.Parse(dr["dgnhap"].ToString()));
                                //slyeucau = slyeucau - float.Parse(dr["slchuaxuat"].ToString());
                                float slchuaxuat = float.Parse(dr["slchuaxuat"].ToString()) - slyeucau;

                                sql = "update PhieuNhapCTTam  set slchuaxuat = " + slchuaxuat + " where manhapxuatct = " + submanhapxuatct + " and manhapxuat = '" + submanhapxuat + "' and mahang = '" + mahang + "'";

                                if (executeQuery(sql) == false)
                                {
                                    ok = false;
                                    break;
                                }
                                break;
                            }
                            else
                            {
                                tgban = tgban + (int)(float.Parse(dr["slchuaxuat"].ToString()) * int.Parse(dr["dgnhap"].ToString()));
                                slyeucau = slyeucau - float.Parse(dr["slchuaxuat"].ToString());

                                sql = "update PhieuNhapCTTam  set slchuaxuat = 0 where manhapxuatct = " + submanhapxuatct + " and  manhapxuat = '" + submanhapxuat + "' and mahang = '" + mahang + "'";
                                if (executeQuery(sql) == false)
                                {
                                    ok = false;
                                    break;
                                }
                            }
                        }
                        if (ok)
                        {

                            if (slnhapxuat != 0)
                            {
                                dgbanvon = (int)(tgban / slnhapxuat);
                            }
                            else
                                dgbanvon = 0;

                            string sqlthempxtam = "insert into PhieuXuatCTTam(manhapxuatct,manhapxuat,mahang,slxuat,dgxuatvon) ";
                            sqlthempxtam += " values (" + manhapxuatct + ",'" + manhapxuat + "','" + mahang + "'," + slnhapxuat + "," + dgbanvon + ")";
                            if (executeQuery(sqlthempxtam) == false)

                                ok = false;

                        }


                    }
                }



            }
            catch (Exception ex)
            {

                UltitityStatic.thongBaoLoi(ex.Message + " Mã hàng: " + mahang);
            }
            return ok;
            
             
        }
        public Boolean nhapTruocXuatTruoc(string mahang)
        {
            //string update1 = "update PhieuNhapXuatCT set slchuaxuat = slnhapxuat where mahang = '" + mahang + "'";
            //if (executeQuery(update1) == false)
            //    return false;
            
            //string dspx = "select a.manhapxuat, mahang, slnhapxuat, (dgnhapxuat/slquydoi) as dgnhapxuat from PhieuNhapXuatCT a, PhieuNhapXuat b where a.mahang = '" + mahang + "' and a.manhapxuat = b.manhapxuat and loainhapxuat = 'PX' order by manhapxuat";



            //Boolean ok = true;
            //DataTable dtpx = getDataTable(dspx);
            //if (dtpx.Rows.Count > 0)
            //{

            //    foreach (DataRow row_px in dtpx.Rows)
            //    {


            //        string manhapxuat = row_px["manhapxuat"].ToString().Trim();
            //        double slnhapxuat = double.Parse(row_px["slnhapxuat"].ToString().Trim());
            //        string pnchuaxuathet = "select a.manhapxuat,mahang,dgnhapxuat,slchuaxuat ";
            //        pnchuaxuathet += "from PhieuNhapXuatCT a, PhieuNhapXuat b where a.manhapxuat = b.manhapxuat and mahang = '" + mahang + "' and slchuaxuat > 0 and loainhapxuat = 'PN' order by sort";
            //        double slyeucau = slnhapxuat;
            //        double dgbanvon = 0;
            //        double tgban = 0;
            //        DataTable dtpn = getDataTable(pnchuaxuathet);
            //        if (dtpx.Rows.Count > 0)
            //        {
            //            foreach (DataRow dr in dtpn.Rows)
            //            {
            //                string sql = "";
            //                string submanhapxuat = dr["manhapxuat"].ToString();

            //                if (slyeucau - double.Parse(dr["slchuaxuat"].ToString()) <= 0)
            //                {
            //                    tgban = tgban + (double)(slyeucau * double.Parse(dr["dgnhapxuat"].ToString()));
            //                    slyeucau = slyeucau - double.Parse(dr["slchuaxuat"].ToString());
            //                    double slchuaxuat = double.Parse(dr["slchuaxuat"].ToString()) - slyeucau;

            //                    sql = "update PhieuNhapXuatCT  set slchuaxuat = " + slchuaxuat + " where  manhapxuat = '" + submanhapxuat + "' and mahang = '" + mahang + "'";
            //                    if (executeQuery(sql) == false)
            //                    {
            //                        ok = false;
            //                        break;
            //                    }
            //                    break;
            //                }
            //                else
            //                {
            //                    tgban = tgban + (double)(double.Parse(dr["slchuaxuat"].ToString()) * double.Parse(dr["dgnhapxuat"].ToString()));
            //                    slyeucau = slyeucau - double.Parse(dr["slchuaxuat"].ToString());

            //                    sql = "update PhieuNhapXuatCT  set slchuaxuat = 0 where manhapxuat = '" + submanhapxuat + "' and mahang = '" + mahang + "'";
            //                    if (executeQuery(sql) == false)
            //                    {
            //                        ok = false;
            //                        break;
            //                    }
            //                }
            //            }
            //            if (ok)
            //            {
            //                //PhieuXuatCTTam clspxtam = new PhieuXuatCTTam();
            //                if (slnhapxuat != 0)
            //                    dgbanvon = System.Math.Round((tgban / slnhapxuat), 0);
            //                else dgbanvon = 0;
            //                // UltitityStatic.thongBaoThanhCong(dgbanvon.ToString() + " -" + slnhapxuat.ToString());
            //                string sqlthempxtam = "update PhieuNhapXuatCT set dgxuatvon = " + dgbanvon + " where manhapxuat = '" + manhapxuat + "' and mahang = '" + mahang + "'";

            //                if (executeQuery(sqlthempxtam) == false)
            //                    ok = false;
            //            }
            //        }
            //    }
            //}


            //return true;
            string update1 = "update PhieuNhapXuatCT set slchuaxuat = slnhapxuat where mahang = '" + mahang + "'";
            if (executeQuery(update1) == false)
                return false;

            string dspx = "select distinct  manhapxuatct,a.manhapxuat, mahang, slnhapxuat, convert(decimal,(dgnhapxuat/slquydoi)) as dgnhapxuat from PhieuNhapXuatCT a, PhieuNhapXuat b where a.mahang = '" + mahang + "' and a.manhapxuat = b.manhapxuat and loainhapxuat = 'PX' order by manhapxuat";
            Boolean ok = true;         
            DataTable dtpx = getDataTable(dspx);
            if (dtpx.Rows.Count > 0)
            {

                foreach (DataRow row_px in dtpx.Rows)
                {

                    int manhapxuatct = 0;

                    manhapxuatct = int.Parse(row_px["manhapxuatct"].ToString());
                    string manhapxuat = row_px["manhapxuat"].ToString().Trim();
                    float slnhapxuat = float.Parse(row_px["slnhapxuat"].ToString().Trim());
                    string pnchuaxuathet = "select distinct manhapxuatct,a.manhapxuat,mahang,convert(decimal,dgnhapxuat/slquydoi) as dgnhap,slchuaxuat,sort ";
                    pnchuaxuathet += "from PhieuNhapXuatCT a, PhieuNhapXuat b where a.manhapxuat = b.manhapxuat and mahang = '" + mahang + "' and slchuaxuat > 0 and loainhapxuat = 'PN' order by sort";
                    float slyeucau = slnhapxuat;
                    int dgbanvon = 0;
                    int tgban = 0;
                    DataTable dtpn = getDataTable(pnchuaxuathet);

                    foreach (DataRow dr in dtpn.Rows)
                    {
                        string sql = "";
                        int submanhapxuatct = int.Parse(dr["manhapxuatct"].ToString());
                        string submanhapxuat = dr["manhapxuat"].ToString();

                        if (slyeucau - float.Parse(dr["slchuaxuat"].ToString()) <= 0)
                        {
                            tgban = tgban + (int)(slyeucau * int.Parse(dr["dgnhap"].ToString()));
                            //slyeucau = slyeucau - float.Parse(dr["slchuaxuat"].ToString());
                            float slchuaxuat = float.Parse(dr["slchuaxuat"].ToString()) - slyeucau;

                            sql = "update PhieuNhapXuatCT  set slchuaxuat = " + slchuaxuat + " where manhapxuatct = " + submanhapxuatct + " and manhapxuat = '" + submanhapxuat + "' and mahang = '" + mahang + "'";

                            if (executeQuery(sql) == false)
                            {
                                ok = false;
                                break;
                            }
                            break;
                        }
                        else
                        {
                            tgban = tgban + (int)(float.Parse(dr["slchuaxuat"].ToString()) * int.Parse(dr["dgnhap"].ToString()));
                            slyeucau = slyeucau - float.Parse(dr["slchuaxuat"].ToString());

                            sql = "update PhieuNhapXuatCT  set slchuaxuat = 0 where manhapxuatct = " + submanhapxuatct + " and  manhapxuat = '" + submanhapxuat + "' and mahang = '" + mahang + "'";
                            if (executeQuery(sql) == false)
                            {
                                ok = false;
                                break;
                            }
                        }
                    }
                    if (ok)
                    {

                        if (slnhapxuat != 0)
                        {
                            dgbanvon = (int)(tgban / slnhapxuat);
                        }
                        else
                            dgbanvon = 0;

                        string sqlthempxtam = "update PhieuNhapXuatCT set   dgxuatvon = " + dgbanvon + " where manhapxuatct = " + manhapxuatct + " and manhapxuat = '" + manhapxuat + "' and mahang = '" + mahang + "'";
                        if (executeQuery(sqlthempxtam) == false)

                            ok = false;

                    }


                }
            }



           
            return ok;
        }
        public Boolean tinhGiaVonTrenDonHang(string manhapuxat,string mahang)
        {
            
            //string update1 = "update PhieuNhapXuatCT set slchuaxuat = slnhapxuat where mahang = '" + mahang + "'";
            //if (executeQuery(update1) == false)
            //    return false;

            string dspx = "select distinct  manhapxuatct,a.manhapxuat, mahang, slnhapxuat, convert(decimal,(dgnhapxuat/slquydoi)) as dgnhapxuat from PhieuNhapXuatCT a, PhieuNhapXuat b where a.mahang = '" + mahang + "' and a.manhapxuat = '" + manhapuxat + "' and a.manhapxuat = b.manhapxuat and loainhapxuat = 'PX' order by manhapxuat";
            Boolean ok = true;
            DataTable dtpx = getDataTable(dspx);
            if (dtpx.Rows.Count > 0)
            {

                foreach (DataRow row_px in dtpx.Rows)
                {

                    int manhapxuatct = 0;

                    manhapxuatct = int.Parse(row_px["manhapxuatct"].ToString());
                    string manhapxuat = row_px["manhapxuat"].ToString().Trim();
                    float slnhapxuat = float.Parse(row_px["slnhapxuat"].ToString().Trim());
                    string pnchuaxuathet = "select distinct manhapxuatct,a.manhapxuat,mahang,convert(decimal,dgnhapxuat/slquydoi) as dgnhap,slchuaxuat,sort ";
                    pnchuaxuathet += "from PhieuNhapXuatCT a, PhieuNhapXuat b where a.manhapxuat = b.manhapxuat and mahang = '" + mahang + "' and slchuaxuat > 0 and loainhapxuat = 'PN' order by sort";
                    float slyeucau = slnhapxuat;
                    int dgbanvon = 0;
                    int tgban = 0;
                    DataTable dtpn = getDataTable(pnchuaxuathet);

                    foreach (DataRow dr in dtpn.Rows)
                    {
                        string sql = "";
                        int submanhapxuatct = int.Parse(dr["manhapxuatct"].ToString());
                        string submanhapxuat = dr["manhapxuat"].ToString();

                        if (slyeucau - float.Parse(dr["slchuaxuat"].ToString()) <= 0)
                        {
                            tgban = tgban + (int)(slyeucau * int.Parse(dr["dgnhap"].ToString()));
                            //slyeucau = slyeucau - float.Parse(dr["slchuaxuat"].ToString());
                            float slchuaxuat = float.Parse(dr["slchuaxuat"].ToString()) - slyeucau;

                            sql = "update PhieuNhapXuatCT  set slchuaxuat = " + slchuaxuat + " where manhapxuatct = " + submanhapxuatct + " and manhapxuat = '" + submanhapxuat + "' and mahang = '" + mahang + "'";

                            if (executeQuery(sql) == false)
                            {
                                ok = false;
                                break;
                            }
                            break;
                        }
                        else
                        {
                            tgban = tgban + (int)(float.Parse(dr["slchuaxuat"].ToString()) * int.Parse(dr["dgnhap"].ToString()));
                            slyeucau = slyeucau - float.Parse(dr["slchuaxuat"].ToString());

                            sql = "update PhieuNhapXuatCT  set slchuaxuat = 0 where manhapxuatct = " + submanhapxuatct + " and  manhapxuat = '" + submanhapxuat + "' and mahang = '" + mahang + "'";
                            if (executeQuery(sql) == false)
                            {
                                ok = false;
                                break;
                            }
                        }
                    }
                    if (ok)
                    {

                        if (slnhapxuat != 0)
                        {
                            dgbanvon = (int)(tgban / slnhapxuat);
                        }
                        else
                            dgbanvon = 0;
                        
                        string sqlthempxtam = "update PhieuNhapXuatCT set   dgxuatvon = " + dgbanvon + " where manhapxuatct = " + manhapxuatct + " and manhapxuat = '" + manhapxuat + "' and mahang = '" + mahang + "'";
                        if (executeQuery(sqlthempxtam) == false)

                            ok = false;

                    }


                }
            }




            return ok;
        }
        public Boolean chinhSuaSLNhapXuat(string ngay)
        {
            string sql = "update PhieuNhapXuatCT set slnhapxuat = slchuaquydoi * slquydoi,slchuaxuat = slnhapxuat from PhieuNhapXuatCT a left join PhieuNhapXuat b where a.manhapxuat = b.manhapxuat where convert(datetime,ngaynhapxuat,103) < convert(datetime,'" + ngay + "',103)";
            return executeQuery(sql);
        }
        //public Boolean tinhGiaVonTrongKhoangThoiGian(string tungay, string denngay)
        //{

        //    //string update1 = "update PhieuNhapXuatCT set slchuaxuat = slnhapxuat where mahang = '" + mahang + "'";
        //    //if (executeQuery(update1) == false)
        //    //    return false;

        //    string dspx = "select distinct  manhapxuatct,a.manhapxuat, mahang, slnhapxuat, convert(decimal,(dgnhapxuat/slquydoi)) as dgnhapxuat from PhieuNhapXuatCT a, PhieuNhapXuat b where a.mahang = '" + mahang + "' and a.manhapxuat = '" + manhapuxat + "' and a.manhapxuat = b.manhapxuat and loainhapxuat = 'PX' order by manhapxuat";
        //    Boolean ok = true;
        //    DataTable dtpx = getDataTable(dspx);
        //    if (dtpx.Rows.Count > 0)
        //    {

        //        foreach (DataRow row_px in dtpx.Rows)
        //        {

        //            int manhapxuatct = 0;

        //            manhapxuatct = int.Parse(row_px["manhapxuatct"].ToString());
        //            string manhapxuat = row_px["manhapxuat"].ToString().Trim();
        //            float slnhapxuat = float.Parse(row_px["slnhapxuat"].ToString().Trim());
        //            string pnchuaxuathet = "select distinct manhapxuatct,a.manhapxuat,mahang,convert(decimal,dgnhapxuat/slquydoi) as dgnhap,slchuaxuat,sort ";
        //            pnchuaxuathet += "from PhieuNhapXuatCT a, PhieuNhapXuat b where a.manhapxuat = b.manhapxuat and mahang = '" + mahang + "' and slchuaxuat > 0 and loainhapxuat = 'PN' order by sort";
        //            float slyeucau = slnhapxuat;
        //            int dgbanvon = 0;
        //            int tgban = 0;
        //            DataTable dtpn = getDataTable(pnchuaxuathet);

        //            foreach (DataRow dr in dtpn.Rows)
        //            {
        //                string sql = "";
        //                int submanhapxuatct = int.Parse(dr["manhapxuatct"].ToString());
        //                string submanhapxuat = dr["manhapxuat"].ToString();

        //                if (slyeucau - float.Parse(dr["slchuaxuat"].ToString()) <= 0)
        //                {
        //                    tgban = tgban + (int)(slyeucau * int.Parse(dr["dgnhap"].ToString()));
        //                    //slyeucau = slyeucau - float.Parse(dr["slchuaxuat"].ToString());
        //                    float slchuaxuat = float.Parse(dr["slchuaxuat"].ToString()) - slyeucau;

        //                    sql = "update PhieuNhapXuatCT  set slchuaxuat = " + slchuaxuat + " where manhapxuatct = " + submanhapxuatct + " and manhapxuat = '" + submanhapxuat + "' and mahang = '" + mahang + "'";

        //                    if (executeQuery(sql) == false)
        //                    {
        //                        ok = false;
        //                        break;
        //                    }
        //                    break;
        //                }
        //                else
        //                {
        //                    tgban = tgban + (int)(float.Parse(dr["slchuaxuat"].ToString()) * int.Parse(dr["dgnhap"].ToString()));
        //                    slyeucau = slyeucau - float.Parse(dr["slchuaxuat"].ToString());

        //                    sql = "update PhieuNhapXuatCT  set slchuaxuat = 0 where manhapxuatct = " + submanhapxuatct + " and  manhapxuat = '" + submanhapxuat + "' and mahang = '" + mahang + "'";
        //                    if (executeQuery(sql) == false)
        //                    {
        //                        ok = false;
        //                        break;
        //                    }
        //                }
        //            }
        //            if (ok)
        //            {

        //                if (slnhapxuat != 0)
        //                {
        //                    dgbanvon = (int)(tgban / slnhapxuat);
        //                }
        //                else
        //                    dgbanvon = 0;

        //                string sqlthempxtam = "update PhieuNhapXuatCT set   dgxuatvon = " + dgbanvon + " where manhapxuatct = " + manhapxuatct + " and manhapxuat = '" + manhapxuat + "' and mahang = '" + mahang + "'";
        //                if (executeQuery(sqlthempxtam) == false)

        //                    ok = false;

        //            }


        //        }
        //    }




        //    return ok;
        //}
        public void tinhGiaVon(string mahang,string manhapxuat,float slnhapxuat)
        {
            Boolean ok = true;
            double manhapxuatct = getNumberValue("select manhapxuatct from PhieuNhapXuatct where manhapxuat = '" + manhapxuat + "' and mahang = '" + mahang + "' and slnhapxuat = " + slnhapxuat + "");
            string pnchuaxuathet = "select distinct manhapxuatct,a.manhapxuat,mahang,convert(decimal,dgnhapxuat/slquydoi) as dgnhap,slchuaxuat,sort ";
            pnchuaxuathet += "from PhieuNhapXuatCT a, PhieuNhapXuat b where a.manhapxuat = b.manhapxuat and mahang = '" + mahang + "' and slchuaxuat > 0 and loainhapxuat = 'PN' order by sort";
            float slyeucau = slnhapxuat;
            int dgbanvon = 0;
            int tgban = 0;
            DataTable dtpn = getDataTable(pnchuaxuathet);

            foreach (DataRow dr in dtpn.Rows)
            {
                string sql = "";
                int submanhapxuatct = int.Parse(dr["manhapxuatct"].ToString());
                string submanhapxuat = dr["manhapxuat"].ToString();

                if (slyeucau - float.Parse(dr["slchuaxuat"].ToString()) <= 0)
                {
                    tgban = tgban + (int)(slyeucau * int.Parse(dr["dgnhap"].ToString()));
                    //slyeucau = slyeucau - float.Parse(dr["slchuaxuat"].ToString());
                    float slchuaxuat = float.Parse(dr["slchuaxuat"].ToString()) - slyeucau;

                    sql = "update PhieuNhapXuatCT  set slchuaxuat = " + slchuaxuat + " where manhapxuatct = " + submanhapxuatct + " and manhapxuat = '" + submanhapxuat + "' and mahang = '" + mahang + "'";

                    if (executeQuery(sql) == false)
                    {
                        ok = false;
                        break;
                    }
                    break;
                }
                else
                {
                    tgban = tgban + (int)(float.Parse(dr["slchuaxuat"].ToString()) * int.Parse(dr["dgnhap"].ToString()));
                    slyeucau = slyeucau - float.Parse(dr["slchuaxuat"].ToString());

                    sql = "update PhieuNhapXuatCT  set slchuaxuat = 0 where manhapxuatct = " + submanhapxuatct + " and  manhapxuat = '" + submanhapxuat + "' and mahang = '" + mahang + "'";
                    if (executeQuery(sql) == false)
                    {
                        ok = false;
                        break;
                    }
                }
            }
            if (ok)
            {

                if (slnhapxuat != 0)
                {
                    dgbanvon = (int)(tgban / slnhapxuat);
                }
                else
                    dgbanvon = 0;

                string sqlthempxtam = "update PhieuNhapXuatCT set   dgxuatvon = " + dgbanvon + " where manhapxuatct = " + manhapxuatct + " and manhapxuat = '" + manhapxuat + "' and mahang = '" + mahang + "'";
                if (executeQuery(sqlthempxtam) == false)

                    ok = false;

            }

        }
        public void tinhGiaVonToanHeThong()
        {
            PhieuNhapXuat cls = new PhieuNhapXuat();
            TinhGiaVon clsgiavon = new TinhGiaVon();          
            foreach (DataRow row in cls.getDataTable("select distinct mahang from PhieuNhapXuatCT").Rows)
            {
                clsgiavon.nhapTruocXuatTruoc(row["mahang"].ToString());

            }
            
        }
    }
}
